package org.cbyyx.tc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.cbyyx.tc.annotation.AuthCheck;
import org.cbyyx.tc.common.BaseResponse;
import org.cbyyx.tc.enums.ErrorCode;
import org.cbyyx.tc.common.ResultUtils;
import org.cbyyx.tc.constant.UserConstant;
import org.cbyyx.tc.exception.ThrowUtils;
import org.cbyyx.tc.model.dto.productRequest.ProductCreateRequest;
import org.cbyyx.tc.model.dto.productRequest.ProductQueryRequest;
import org.cbyyx.tc.model.dto.productRequest.ProductUpdateRequest;
import org.cbyyx.tc.model.vo.productResponse.ProductDetailVO;
import org.cbyyx.tc.model.vo.productResponse.ProductsVO;
import org.cbyyx.tc.service.ProductsService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品管理
 */
@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
public class ProductController {
    
    private final ProductsService productsService;

    /**
     * 创建商品
     * @param request 商品创建请求参数
     * @return 创建结果
     */
    @PostMapping
    @AuthCheck(role = UserConstant.MERCHANT)
    public BaseResponse<Boolean> createProduct(@RequestBody @Valid ProductCreateRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求参数为空");
        return ResultUtils.success(productsService.createProduct(request));
    }

    /**
     * 获取商品详情
     * @param id 商品ID
     * @return 商品详情
     */
    @GetMapping("/{id}")
    public BaseResponse<ProductDetailVO> getProduct(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "商品ID无效");
        return ResultUtils.success(productsService.getProductDetail(id));
    }

    /**
     * 更新商品信息
     * @param id 商品ID
     * @param request 商品更新请求参数
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @AuthCheck(role = UserConstant.MERCHANT)
    public BaseResponse<Boolean> updateProduct(@PathVariable Long id, @RequestBody @Valid ProductUpdateRequest request) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "商品ID无效");
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求参数为空");
        request.setId(id);
        return ResultUtils.success(productsService.updateProduct(request));
    }

    /**
     * 更新商品库存
     * @param id 商品ID
     * @param quantity 库存数量
     * @return 更新结果
     */
    @PutMapping("/{id}/stock")
    @AuthCheck(role = UserConstant.MERCHANT)
    public BaseResponse<Boolean> updateStock(@PathVariable Long id, @RequestParam Integer quantity) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "商品ID无效");
        ThrowUtils.throwIf(quantity == null || quantity < 0, ErrorCode.PARAMS_ERROR, "库存数量无效");
        return ResultUtils.success(productsService.updateStock(id, quantity));
    }

    /**
     * 调整商品价格
     * @param id 商品ID
     * @param price 新价格
     * @return 调整结果
     */
    @PutMapping("/{id}/price")
    @AuthCheck(role = UserConstant.MERCHANT)
    public BaseResponse<Boolean> updatePrice(@PathVariable Long id, @RequestParam Long price) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "商品ID无效");
        ThrowUtils.throwIf(price == null || price <= 0, ErrorCode.PARAMS_ERROR, "价格无效");
        return ResultUtils.success(productsService.updatePrice(id, price));
    }

    /**
     * 更新商品状态
     * @param id 商品ID
     * @param status 新状态
     * @return 更新结果
     */
    @PutMapping("/{id}/status")
    @AuthCheck(role = UserConstant.MERCHANT)
    public BaseResponse<Boolean> updateStatus(@PathVariable Long id, @RequestParam String status) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "商品ID无效");
        ThrowUtils.throwIf(StringUtils.isBlank(status), ErrorCode.PARAMS_ERROR, "状态不能为空");
        return ResultUtils.success(productsService.updateStatus(id, status));
    }

    /**
     * 删除商品
     * @param id 商品ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @AuthCheck(role = UserConstant.MERCHANT)
    public BaseResponse<Boolean> deleteProduct(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "商品ID无效");
        return ResultUtils.success(productsService.deleteProduct(id));
    }

    /**
     * 获取商品分页列表
     * @param request 查询条件
     * @return 商品分页列表
     */
    @GetMapping("/list")
    public BaseResponse<Page<ProductsVO>> listProducts(ProductQueryRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求参数为空");
        return ResultUtils.success(productsService.listProducts(request));
    }

    /**
     * 查询商品库存
     * @param id 商品ID
     * @return 库存数量
     */
    @GetMapping("/{id}/stock")
    @AuthCheck(role = UserConstant.USER)
    public BaseResponse<Integer> getStock(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "商品ID无效");
        return ResultUtils.success(productsService.getStock(id));
    }

    /**
     * 搜索商品
     * @param keyword 搜索关键词
     * @return 搜索结果分页列表
     */
    @GetMapping("/search")
    public BaseResponse<Page<ProductsVO>> searchProducts(@RequestParam String keyword) {
        ThrowUtils.throwIf(StringUtils.isBlank(keyword), ErrorCode.PARAMS_ERROR, "搜索关键词不能为空");
        return ResultUtils.success(productsService.searchProducts(keyword));
    }

    /**
     * 按分类查询商品
     * @param categoryId 分类ID
     * @return 商品分页列表
     */
    @GetMapping("/category/{categoryId}")
    public BaseResponse<Page<ProductsVO>> getProductsByCategory(@PathVariable Long categoryId) {
        ThrowUtils.throwIf(categoryId == null || categoryId <= 0, ErrorCode.PARAMS_ERROR, "分类ID无效");
        return ResultUtils.success(productsService.getProductsByCategory(categoryId));
    }

    /**
     * 获取热门商品列表
     * @return 热门商品分页列表
     */
    @GetMapping("/hot")
    public BaseResponse<Page<ProductsVO>> getHotProducts() {
        return ResultUtils.success(productsService.getHotProducts());
    }
}